package com.tp;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.google.android.maps.GeoPoint;

import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class SearchObject extends CustomWindow implements OnClickListener,
		LocationListener {
	LocationManager lm = null;
	GeoPoint p = null;
	static float latitude;
	static float longitude;
	static ArrayList<String> result;
	static JSONArray jArr;
	EditText name;
	String type;
	EditText distance;
	EditText amount;
	Spinner spinner;
	int amount_;
	boolean gps_enabled = false;
	boolean network_enabled = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		String a[] = { "", "Khách sạn", "Nhà hàng", "Sự kiện" };
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search);
		this.icon.setImageResource(R.drawable.search);
		this.title.setText("Tìm đối tượng");
		View search_btn = (findViewById(R.id.search_btn));
		search_btn.setOnClickListener(this);

		spinner = (Spinner) findViewById(R.id.search_type);
		// Create an ArrayAdapter using the string array and a default spinner
		// layout
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, a);
		// Specify the layout to use when the list of choices appears
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		// Apply the adapter to the spinner
		spinner.setAdapter(adapter);

		name = (EditText) findViewById(R.id.search_name);
		distance = (EditText) findViewById(R.id.search_distance);
		amount = (EditText) findViewById(R.id.search_amount);

		lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
		network_enabled = lm
				.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
		if (!gps_enabled && !network_enabled) {
			Toast.makeText(this, "GPS va NetWork khong enable",
					Toast.LENGTH_SHORT).show();
		}
		if (network_enabled) {
			lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
					this);
			Toast.makeText(this, "NetWork is enable",
					Toast.LENGTH_SHORT).show();
		}
		if (gps_enabled){
			lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
			Toast.makeText(this, "GPS is enable",
					Toast.LENGTH_SHORT).show();
		}


	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_search, menu);
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.search_btn:

			result = sendRequest();
			if (result.size() > 0)
				startActivity(new Intent(this, ListObject.class));
			else {
				Toast.makeText(this, "Not found", Toast.LENGTH_SHORT).show();
			}
			break;

		}

	}

	public ArrayList<String> sendRequest() {
		// TODO Auto-generated method stub

		JSONArray jArray;
		JSONObject json_data = null;
		int i;
		ArrayList<String> r = new ArrayList<String>();
		ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
		postParameters.add(new BasicNameValuePair("type", Integer
				.toString(spinner.getSelectedItemPosition())));
		postParameters.add(new BasicNameValuePair("name", name.getText()
				.toString()));
		postParameters.add(new BasicNameValuePair("distance", distance
				.getText().toString()));

		postParameters.add(new BasicNameValuePair("latitude", Float
				.toString(latitude)));
		postParameters.add(new BasicNameValuePair("longitude", Float
				.toString(longitude)));
		String result = null;
		try {

			result = CustomHttpClient.executeHttpPost(Constant.host
					+ "/SearchController/index", postParameters);

			jArray = new JSONArray(result);
			jArr = jArray;
			if (!amount.getText().toString().equals("")) {
				if (Integer.parseInt((amount.getText().toString())) > jArray
						.length()) {
					amount_ = jArray.length();
				} else
					amount_ = Integer.parseInt((amount.getText().toString()));
			} else
				amount_ = jArray.length();
			for (i = 0; i < amount_; i++) {
				json_data = jArray.getJSONObject(i);
				// Log.d("A", json_data.getString("name"));
				r.add(json_data.getString("name"));
			}
			// return r;
		} catch (JSONException e1) {
			e1.printStackTrace();
			// Toast.makeText(((ContextWrapper) activity).getBaseContext(),
			// e1.toString(), Toast.LENGTH_SHORT).show();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return r;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.location.LocationListener#onLocationChanged(android.location.
	 * Location)
	 */
	@Override
	public void onLocationChanged(Location loc) {
		// TODO Auto-generated method stub
		lm.removeUpdates(this);
		latitude = (float) loc.getLatitude();
		longitude = (float) loc.getLongitude();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.location.LocationListener#onProviderDisabled(java.lang.String)
	 */
	@Override
	public void onProviderDisabled(String arg0) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.location.LocationListener#onProviderEnabled(java.lang.String)
	 */
	@Override
	public void onProviderEnabled(String arg0) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.location.LocationListener#onStatusChanged(java.lang.String,
	 * int, android.os.Bundle)
	 */
	@Override
	public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
		// TODO Auto-generated method stub

	}

}
